﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
   public class GameService
    {
        //新增
        public static bool AddGame(Game gm)
        {
            SqlParameter[] para = new SqlParameter[] { new SqlParameter("@Gm_Name",gm.Gm_Name), new SqlParameter("@Gm_TypeId",gm.Gm_TypeId),
            new SqlParameter("@Gm_Price",gm.Gm_Price),new SqlParameter("@Gm_Image",gm.Gm_Image),new SqlParameter("@Gm_Desc",gm.Gm_Desc),
            new SqlParameter("@Gm_Upcount",gm.Gm_Upcount),new SqlParameter("@Gm_Lv",gm.Gm_Lv)};
            return DBHelper.Instance().ExcuteProcedure("up_AddGame", para);
        }
        
       //删除
        public static bool DeleGame(int id)
        {
            SqlParameter[] para = new SqlParameter[] { new SqlParameter("@Gm_Id", id) };
            return DBHelper.Instance().ExcuteProcedure("up_DeleGame", para);
        }
        
       //修改全部内容
        public static bool UpdaGame(Game gm)
        {
            SqlParameter[] para = new SqlParameter[] { new SqlParameter("@Gm_Id",gm.Gm_Id), new SqlParameter("@Gm_Name",gm.Gm_Name),
            new SqlParameter("@Gm_TypeId",gm.Gm_TypeId),new SqlParameter("@Gm_Price",gm.Gm_Price),new SqlParameter("@Gm_Image",gm.Gm_Image),
            new SqlParameter("@Gm_Lv",gm.Gm_Lv),new SqlParameter("@Gm_Desc",gm.Gm_Desc),new SqlParameter("@Gm_Upcount",gm.Gm_Upcount)};
            return DBHelper.Instance().ExcuteProcedure("up_UpdaGame", para);
        }

        //修改获赞数
        public static bool UpdaCountGame(Game gm)
        {
            SqlParameter[] para = new SqlParameter[] { new SqlParameter("@Gm_Id",gm.Gm_Id),new SqlParameter("@Gm_Upcount",gm.Gm_Upcount)};
            return DBHelper.Instance().ExcuteProcedure("up_UpdaCountGame", para);
        }

       //查询所有游戏信息
        public static List<Game> SeleAllGame()
        {
            List<Game> item = new List<Game>();
            string sql = string.Format("select * from Game ");
            DataTable dt = DBHelper.Instance().GetDataTableBySql(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Game gm = new Game();
                    gm.Gm_Id = Convert.ToInt32(dt.Rows[i]["Gm_Id"].ToString());
                    gm.Gm_Name = dt.Rows[i]["Gm_Name"].ToString();
                    gm.Gm_TypeId = Convert.ToChar(dt.Rows[i]["Gm_TypeId"].ToString());
                    gm.Gm_Price = Convert.ToDouble(dt.Rows[i]["Gm_Price"].ToString());
                    gm.Gm_Image = dt.Rows[i]["Gm_Image"].ToString();
                    gm.Gm_Lv = dt.Rows[i]["Gm_Lv"].ToString();
                    gm.Gm_Desc = dt.Rows[i]["Gm_Desc"].ToString();
                    gm.Gm_Upcount = Convert.ToInt32(dt.Rows[i]["Gm_Upcount"].ToString());
                    item.Add(gm);
                }
            }
            return item;
        }
        
       //查指定游戏id信息
        public static List<Game> SeleIdGame(int id)
        {
            List<Game> item = new List<Game>();
            string sql = string.Format("select * from Game where Gm_Id='{0}'",id);
            DataTable dt = DBHelper.Instance().GetDataTableBySql(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Game gm = new Game();
                    gm.Gm_Id = Convert.ToInt32(dt.Rows[i]["Gm_Id"].ToString());
                    gm.Gm_Name = dt.Rows[i]["Gm_Name"].ToString();
                    gm.Gm_TypeId = Convert.ToChar(dt.Rows[i]["Gm_TypeId"].ToString());
                    gm.Gm_Price = Convert.ToDouble(dt.Rows[i]["Gm_Price"].ToString());
                    gm.Gm_Image = dt.Rows[i]["Gm_Image"].ToString();
                    gm.Gm_Lv = dt.Rows[i]["Gm_Lv"].ToString();
                    gm.Gm_Desc = dt.Rows[i]["Gm_Desc"].ToString();
                    gm.Gm_Upcount = Convert.ToInt32(dt.Rows[i]["Gm_Upcount"].ToString());
                    item.Add(gm);
                }
            }
            return item;
        }

        //查指定游戏类型id信息
        public static List<Game> SeleTypeIdGame(int id)
        {
            List<Game> item = new List<Game>();
            string sql = string.Format("select * from Game where Gm_TypeId='{0}'", id);
            DataTable dt = DBHelper.Instance().GetDataTableBySql(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Game gm = new Game();
                    gm.Gm_Id = Convert.ToInt32(dt.Rows[i]["Gm_Id"].ToString());
                    gm.Gm_Name = dt.Rows[i]["Gm_Name"].ToString();
                    gm.Gm_TypeId = Convert.ToChar(dt.Rows[i]["Gm_TypeId"].ToString());
                    gm.Gm_Price = Convert.ToDouble(dt.Rows[i]["Gm_Price"].ToString());
                    gm.Gm_Image = dt.Rows[i]["Gm_Image"].ToString();
                    gm.Gm_Lv = dt.Rows[i]["Gm_Lv"].ToString();
                    gm.Gm_Desc = dt.Rows[i]["Gm_Desc"].ToString();
                    gm.Gm_Upcount = Convert.ToInt32(dt.Rows[i]["Gm_Upcount"].ToString());
                    item.Add(gm);
                }
            }
            return item;
        }

       //取10个热门游戏
        public static List<Game> SeleTopshiGame()
        {
            List<Game> item = new List<Game>();
            string sql = string.Format("select top 10 * from Game order by Gm_Upcount desc");
            DataTable dt = DBHelper.Instance().GetDataTableBySql(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Game gm = new Game();
                    gm.Gm_Id = Convert.ToInt32(dt.Rows[i]["Gm_Id"].ToString());
                    gm.Gm_Name = dt.Rows[i]["Gm_Name"].ToString();
                    gm.Gm_TypeId = Convert.ToChar(dt.Rows[i]["Gm_TypeId"].ToString());
                    gm.Gm_Price = Convert.ToDouble(dt.Rows[i]["Gm_Price"].ToString());
                    gm.Gm_Image = dt.Rows[i]["Gm_Image"].ToString();
                    gm.Gm_Lv = dt.Rows[i]["Gm_Lv"].ToString();
                    gm.Gm_Desc = dt.Rows[i]["Gm_Desc"].ToString();
                    gm.Gm_Upcount = Convert.ToInt32(dt.Rows[i]["Gm_Upcount"].ToString());
                    item.Add(gm);
                }
            }
            return item;
        }
        
       //热门游戏升序
        public static List<Game> SeleRQShengXuGame()
        {
            List<Game> item = new List<Game>();
            string sql = string.Format("select * from Game order by Gm_Upcount");
            DataTable dt = DBHelper.Instance().GetDataTableBySql(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Game gm = new Game();
                    gm.Gm_Id = Convert.ToInt32(dt.Rows[i]["Gm_Id"].ToString());
                    gm.Gm_Name = dt.Rows[i]["Gm_Name"].ToString();
                    gm.Gm_TypeId = Convert.ToChar(dt.Rows[i]["Gm_TypeId"].ToString());
                    gm.Gm_Price = Convert.ToDouble(dt.Rows[i]["Gm_Price"].ToString());
                    gm.Gm_Image = dt.Rows[i]["Gm_Image"].ToString();
                    gm.Gm_Lv = dt.Rows[i]["Gm_Lv"].ToString();
                    gm.Gm_Desc = dt.Rows[i]["Gm_Desc"].ToString();
                    gm.Gm_Upcount = Convert.ToInt32(dt.Rows[i]["Gm_Upcount"].ToString());
                    item.Add(gm);
                }
            }
            return item;
        }
        
       //热门游戏降序
        public static List<Game> SeleRQJiangXuGame()
        {
            List<Game> item = new List<Game>();
            string sql = string.Format("select * from Game order by Gm_Upcount desc");
            DataTable dt = DBHelper.Instance().GetDataTableBySql(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Game gm = new Game();
                    gm.Gm_Id = Convert.ToInt32(dt.Rows[i]["Gm_Id"].ToString());
                    gm.Gm_Name = dt.Rows[i]["Gm_Name"].ToString();
                    gm.Gm_TypeId = Convert.ToChar(dt.Rows[i]["Gm_TypeId"].ToString());
                    gm.Gm_Price = Convert.ToDouble(dt.Rows[i]["Gm_Price"].ToString());
                    gm.Gm_Image = dt.Rows[i]["Gm_Image"].ToString();
                    gm.Gm_Lv = dt.Rows[i]["Gm_Lv"].ToString();
                    gm.Gm_Desc = dt.Rows[i]["Gm_Desc"].ToString();
                    gm.Gm_Upcount = Convert.ToInt32(dt.Rows[i]["Gm_Upcount"].ToString());
                    item.Add(gm);
                }
            }
            return item;
        }
        
       //查免费游戏信息
        public static List<Game> SeleMianFeiGame()
        {
            List<Game> item = new List<Game>();
            string sql = string.Format("select * from Game where Gm_Price=0");
            DataTable dt = DBHelper.Instance().GetDataTableBySql(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Game gm = new Game();
                    gm.Gm_Id = Convert.ToInt32(dt.Rows[i]["Gm_Id"].ToString());
                    gm.Gm_Name = dt.Rows[i]["Gm_Name"].ToString();
                    gm.Gm_TypeId = Convert.ToChar(dt.Rows[i]["Gm_TypeId"].ToString());
                    gm.Gm_Price = Convert.ToDouble(dt.Rows[i]["Gm_Price"].ToString());
                    gm.Gm_Image = dt.Rows[i]["Gm_Image"].ToString();
                    gm.Gm_Lv = dt.Rows[i]["Gm_Lv"].ToString();
                    gm.Gm_Desc = dt.Rows[i]["Gm_Desc"].ToString();
                    gm.Gm_Upcount = Convert.ToInt32(dt.Rows[i]["Gm_Upcount"].ToString());
                    item.Add(gm);
                }
            }
            return item;
        }
        
       //查付费游戏信息
        public static List<Game> SeleFuFeiGame()
        {
            List<Game> item = new List<Game>();
            string sql = string.Format("select * from Game where Gm_Price>0");
            DataTable dt = DBHelper.Instance().GetDataTableBySql(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Game gm = new Game();
                    gm.Gm_Id = Convert.ToInt32(dt.Rows[i]["Gm_Id"].ToString());
                    gm.Gm_Name = dt.Rows[i]["Gm_Name"].ToString();
                    gm.Gm_TypeId = Convert.ToChar(dt.Rows[i]["Gm_TypeId"].ToString());
                    gm.Gm_Price = Convert.ToDouble(dt.Rows[i]["Gm_Price"].ToString());
                    gm.Gm_Image = dt.Rows[i]["Gm_Image"].ToString();
                    gm.Gm_Lv = dt.Rows[i]["Gm_Lv"].ToString();
                    gm.Gm_Desc = dt.Rows[i]["Gm_Desc"].ToString();
                    gm.Gm_Upcount = Convert.ToInt32(dt.Rows[i]["Gm_Upcount"].ToString());
                    item.Add(gm);
                }
            }
            return item;
        }
        
       //查精选游戏信息
        public static List<Game> SeleJingXuanGame()
        {
            List<Game> item = new List<Game>();
            string sql = string.Format("select * from Game where Gm_Upcount>300");
            DataTable dt = DBHelper.Instance().GetDataTableBySql(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Game gm = new Game();
                    gm.Gm_Id = Convert.ToInt32(dt.Rows[i]["Gm_Id"].ToString());
                    gm.Gm_Name = dt.Rows[i]["Gm_Name"].ToString();
                    gm.Gm_TypeId = Convert.ToChar(dt.Rows[i]["Gm_TypeId"].ToString());
                    gm.Gm_Price = Convert.ToDouble(dt.Rows[i]["Gm_Price"].ToString());
                    gm.Gm_Image = dt.Rows[i]["Gm_Image"].ToString();
                    gm.Gm_Lv = dt.Rows[i]["Gm_Lv"].ToString();
                    gm.Gm_Desc = dt.Rows[i]["Gm_Desc"].ToString();
                    gm.Gm_Upcount = Convert.ToInt32(dt.Rows[i]["Gm_Upcount"].ToString());
                    item.Add(gm);
                }
            }
            return item;
        }
       
       //根据游戏名称模糊查询
        public static List<Game> SeleNameLikeGame(string name)
        {
            List<Game> item = new List<Game>();
            string sql = string.Format("select * from Game where Gm_Name like '%{0}%'",name);
            DataTable dt = DBHelper.Instance().GetDataTableBySql(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Game gm = new Game();
                    gm.Gm_Id = Convert.ToInt32(dt.Rows[i]["Gm_Id"].ToString());
                    gm.Gm_Name = dt.Rows[i]["Gm_Name"].ToString();
                    gm.Gm_TypeId = Convert.ToChar(dt.Rows[i]["Gm_TypeId"].ToString());
                    gm.Gm_Price = Convert.ToDouble(dt.Rows[i]["Gm_Price"].ToString());
                    gm.Gm_Image = dt.Rows[i]["Gm_Image"].ToString();
                    gm.Gm_Lv = dt.Rows[i]["Gm_Lv"].ToString();
                    gm.Gm_Desc = dt.Rows[i]["Gm_Desc"].ToString();
                    gm.Gm_Upcount = Convert.ToInt32(dt.Rows[i]["Gm_Upcount"].ToString());
                    item.Add(gm);
                }
            }
            return item;
        }

        //用户id查询用户下载的游戏
        public static List<Game> SeleGamejoinOrders(int usid)
        {
            List<Game> item = new List<Game>();
            string sql = string.Format("select * from Orders join Game on Orders.Or_GmId=Game.Gm_Id where Or_UsId={0}", usid);
            DataTable dt = DBHelper.Instance().GetDataTableBySql(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Game gm = new Game();
                    gm.Gm_Id = Convert.ToInt32(dt.Rows[i]["Gm_Id"].ToString());
                    gm.Gm_Name = dt.Rows[i]["Gm_Name"].ToString();
                    gm.Gm_TypeId = Convert.ToChar(dt.Rows[i]["Gm_TypeId"].ToString());
                    gm.Gm_Price = Convert.ToDouble(dt.Rows[i]["Gm_Price"].ToString());
                    gm.Gm_Image = dt.Rows[i]["Gm_Image"].ToString();
                    gm.Gm_Lv = dt.Rows[i]["Gm_Lv"].ToString();
                    gm.Gm_Desc = dt.Rows[i]["Gm_Desc"].ToString();
                    gm.Gm_Upcount = Convert.ToInt32(dt.Rows[i]["Gm_Upcount"].ToString());
                    item.Add(gm);
                }
            }
            return item;
        }

        //游戏价格升序
        public static List<Game> SeleJGShengGame()
        {
            List<Game> item = new List<Game>();
            string sql = string.Format("select * from Game order by Gm_Price");
            DataTable dt = DBHelper.Instance().GetDataTableBySql(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Game gm = new Game();
                    gm.Gm_Id = Convert.ToInt32(dt.Rows[i]["Gm_Id"].ToString());
                    gm.Gm_Name = dt.Rows[i]["Gm_Name"].ToString();
                    gm.Gm_TypeId = Convert.ToChar(dt.Rows[i]["Gm_TypeId"].ToString());
                    gm.Gm_Price = Convert.ToDouble(dt.Rows[i]["Gm_Price"].ToString());
                    gm.Gm_Image = dt.Rows[i]["Gm_Image"].ToString();
                    gm.Gm_Lv = dt.Rows[i]["Gm_Lv"].ToString();
                    gm.Gm_Desc = dt.Rows[i]["Gm_Desc"].ToString();
                    gm.Gm_Upcount = Convert.ToInt32(dt.Rows[i]["Gm_Upcount"].ToString());
                    item.Add(gm);
                }
            }
            return item;
        }

        //游戏价格升序
        public static List<Game> SeleJGJiangGame()
        {
            List<Game> item = new List<Game>();
            string sql = string.Format("select * from Game order by Gm_Price desc");
            DataTable dt = DBHelper.Instance().GetDataTableBySql(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Game gm = new Game();
                    gm.Gm_Id = Convert.ToInt32(dt.Rows[i]["Gm_Id"].ToString());
                    gm.Gm_Name = dt.Rows[i]["Gm_Name"].ToString();
                    gm.Gm_TypeId = Convert.ToChar(dt.Rows[i]["Gm_TypeId"].ToString());
                    gm.Gm_Price = Convert.ToDouble(dt.Rows[i]["Gm_Price"].ToString());
                    gm.Gm_Image = dt.Rows[i]["Gm_Image"].ToString();
                    gm.Gm_Lv = dt.Rows[i]["Gm_Lv"].ToString();
                    gm.Gm_Desc = dt.Rows[i]["Gm_Desc"].ToString();
                    gm.Gm_Upcount = Convert.ToInt32(dt.Rows[i]["Gm_Upcount"].ToString());
                    item.Add(gm);
                }
            }
            return item;
        }

   }
}
